<?php

class LoginController extends Controller
{
	public function actionLogin() {   
        $errors   = array();
        $response = array();

		if (Yii::app()->request->isAjaxRequest) {			
            $email    = Yii::app()->request->getPost('email');
            $password = Yii::app()->request->getPost('password');
            
            if (!$email)
                $errors[] = 'Email is missing';
            
            if (!$password) 
                $errors[] = 'Password is missing';	

            if (count($errors) > 0) {
            	$response['errors'] = $errors;
            } else {
                $identity = new ClientIdentity($email, $password);
				if ($identity->authenticate()) {					
					Yii::app()->user->login($identity);
					$response['success'] = 'true';					
				} else {
					$response['errors'] = array('Wrong email or password');
				}
            }   

            echo json_encode($response);
		} else {
            $this->redirect('/site/index');
		}
	}

	public function actionLogout()
	{
		Yii::app()->user->logout();		
		$this->redirect('/site/index');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}